package cn.biplam.back.bean.ddl.sql2db;

import cn.biplam.back.sql.ddl.Table;
import cn.biplam.common.bean.sqlengine.Sql;
import cn.biplam.common.client.DbResource;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 * 功能		把table对象解析成可执行的Sql对象
 * @author 	WangSong
 * @create 	2009-3-24 下午05:07:54
 * @version 1.0
 */
public class TableParser {
	
	/**Table对象*/
	private Table mTable;
	/**数据源信息*/
	private DbResource mDbResource;
	
	/**
	 * 功能：构造方法
	 * @param mTable Table对象
	 */
	public TableParser(DbResource mDbResource,Table mTable){
		this.mDbResource = mDbResource;
		this.mTable = mTable;
	}
	
	/**
	 * 功能   得到表创建语句
	 * @return
	 * @throws ErrorMessage 
	 */
	public Sql getCreate() throws ErrorMessage{
		
		String createTabel = "";
		FieldParser mFieldParser = new FieldParser(this.mDbResource,this.mTable);
		createTabel = mFieldParser.getCreateFiled();

		return new Sql(createTabel);
	}
	
	/**
	 * 功能   得到主键创建语句
	 * @return
	 */
	public Sql getCreatePK() throws ErrorMessage {
		
		String sPk = "";
		PKParser mPKParser = new PKParser(this.mDbResource,this.mTable);
		
		sPk = mPKParser.getCreatePK();

		return new Sql(sPk);
	}
	
	/**
	 * 功能   得到索引创建语句
	 * @return
	 */
	public Sql getCreateIndex()throws ErrorMessage {
		
		String sIndex = "";
		IndexParser mIndexParser = new IndexParser(this.mDbResource,this.mTable);
		
		sIndex = mIndexParser.getCreateIndex();

		return new Sql(sIndex);
	}
	
	/**
	 * 功能   得到外键创建语句
	 * @return
	 */
	public Sql getCreateFK()throws ErrorMessage {
		
		String sFk = "";
		
		FKParser mFKParser = new FKParser(this.mDbResource,this.mTable);
		
		sFk = mFKParser.getCreateFK();

		return new Sql(sFk);
	}
	
	/**
	 * 功能  删除表语句
	 * @return
	 */
	public Sql getDropTable(){
		return new Sql("drop table "+ this.mTable.getPhysicalName());
	}
	
	/**
	 * 功能   得到表删除语句
	 * @return
	 * @throws ErrorMessage 
	 */
	public Sql getDropField() throws ErrorMessage{
		
		String dropField = "";
		FieldParser mFieldParser = new FieldParser(this.mDbResource,this.mTable);
		
		dropField = mFieldParser.getDropFiled();

		return new Sql(dropField);
	}
	
	/**
	 * 功能   得到主键删除语句
	 * @return
	 */
	public Sql getDropPK() throws ErrorMessage {
		
		String sPk = "";
		PKParser mPKParser = new PKParser(this.mDbResource,this.mTable);
		
		sPk = mPKParser.getDropPK();

		return new Sql(sPk);
	}
	
	/**
	 * 功能   得到索引删除语句
	 * @return
	 */
	public Sql getDropIndex()throws ErrorMessage {
		
		String sIndex = "";
		IndexParser mIndexParser = new IndexParser(this.mDbResource,this.mTable);
		
		sIndex = mIndexParser.getDropIndex();

		return new Sql(sIndex);
	}
	
	/**
	 * 功能   得到外键删除语句
	 * @return
	 */
	public Sql getDropFK()throws ErrorMessage {
		
		String sFk = "";
		
		FKParser mFKParser = new FKParser(this.mDbResource,this.mTable);
		
		sFk = mFKParser.getDropFK();

		return new Sql(sFk);
	}

}
